<?php
require "includes/conf.php";
require "includes/functions.php";

$section = "listadiagnos";
$sectiondes = "DIAGN&Oacute;STICOS";
require "includes/header.inc.php";

if ($_POST["modify"])
{
	for ($x=1; $x<=$_POST["numrows"]; $x++)
	{
		$id_diagnos = $_POST["id_diagnos".$x];
		$diagnos = $_POST["diagnos".$x];
		$borrar = $_POST["borrar".$x];

		if ($borrar == "on")
		{
			$query = "DELETE FROM listadiagnos WHERE id_diagnos = '".$id_diagnos."'";
			if (!mysql_query($query, $sock)) $error .= mysql_error($sock).".<br>\n";
		}
		else
		{
			$query = "UPDATE listadiagnos SET diagnos = '".addslashes($diagnos)."' WHERE id_diagnos = '".$id_diagnos."'";
			if (!mysql_query($query, $sock)) $error .= mysql_error($sock).".<br>\n";
		}
	}

	if (isset($error) && $error != "") echo "<br>No se han podido aplicar los cambios porque se han producido los siguientes errores:<br><br><b>".$error."</b>\n";
	else
	{
		echo "<br>Cambios aplicados con &eacute;xito.\n";
		echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=".$_SERVER["PHP_SELF"]."\">\n";
	}
}
else if ($_POST["add"])
{
	$query = "SELECT id_diagnos FROM listadiagnos WHERE diagnos = '".addslashes($_POST["diagnos"])."'";
	$q1 = mysql_query($query, $sock);

	if (mysql_num_rows($q1) > 0)
	{
		echo "<br><b>".$_POST["diagnos"]."</b> ya forma parte de la lista.\n";
		echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=".$_SERVER["PHP_SELF"]."\">\n";
	}
	else
	{
		$query = "INSERT INTO listadiagnos (diagnos) VALUES ('".addslashes($_POST["diagnos"])."')";

		if (!mysql_query($query, $sock)) echo mysql_error($sock).".\n";
		else
		{
			echo "<br><b>".$_POST["diagnos"]."</b> a&ntilde;adido con &eacute;xito.\n";
			echo "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=".$_SERVER["PHP_SELF"]."\">\n";
		}
	}
}
else
{
	echo "<div class=\"subtitle\">Nuevo registro</div>\n";

	echo "<form name=\"form2\" action=\"\" method=\"post\">\n";
	echo "<input type=\"hidden\" name=\"add\" value=\"add\">\n";

	echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"listTable\">\n";

	echo "<tr>\n";
	echo "<td class=\"listFirst\"><input type=\"input\" name=\"diagnos\" class=\"textBox\"></td>\n";
	echo "</tr>\n";

	echo "<tr>\n";
	echo "<td align=\"right\" class=\"listFooterFirst\"><div id=\"bttAdd\" onclick=\"document.form2.submit();\">A&ntilde;adir</div></td>\n";
	echo "</tr>\n";

	echo "</table>\n";
	echo "</form>\n";

	$query = "SELECT id_diagnos, diagnos FROM listadiagnos ORDER BY diagnos";
	$q1 = mysql_query($query, $sock);
	$numrows = mysql_num_rows($q1);

	if ($numrows > 0)
	{
		echo "<br><div class=\"subtitle\">Listado completo</div>\n";

		echo "<form name=\"form1\" action=\"\" method=\"post\">\n";
		echo "<input type=\"hidden\" name=\"modify\" value=\"modify\">\n";
		echo "<input type=\"hidden\" name=\"numrows\" value=\"".$numrows."\">\n";

		echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"listTable\">\n";

		echo "<tr>\n";
		echo "<td width=\"1%\" class=\"listHeaderFirst\">&iquest;Borrar?</td>\n";
		echo "<td class=\"listHeaderLast\">Descripci&oacute;n</td>\n";
		echo "</tr>\n";

		$contador = 1;

		while ($row1 = mysql_fetch_object($q1))
		{
			echo "<input type=\"hidden\" name=\"id_diagnos".$contador."\" value=\"".$row1->id_diagnos."\">\n";
			echo "<tr>\n";
			echo "<td align=\"center\" class=\"listFirst\"><input type=\"checkbox\" name=\"borrar".$contador."\" class=\"checkBox\"></td>\n";
			echo "<td class=\"listLast\"><input type=\"input\" name=\"diagnos".$contador."\" class=\"textBox\" value=\"".htmlentities(stripslashes($row1->diagnos), ENT_QUOTES)."\"></td>\n";
			echo "</tr>\n";

			$contador ++;
		}

		echo "<tr>\n";
		echo "<td class=\"listFooterFirst\">&nbsp;</td>\n";
		echo "<td align=\"right\" class=\"listFooterLast\"><div id=\"bttApply\" onclick=\"document.form1.submit();\">Aplicar cambios</div></td>\n";
		echo "</tr>\n";

		echo "</table>\n";
		echo "</form>\n";
	}

	echo "<script>document.form2.diagnos.focus();</script>\n";
}

require "includes/footer.inc.php";
?>